package com.jk.lesson5;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Created by Auser on 2016/11/16.
 */
public class lesson5_7 {
    public static void main(String...args){
//        test_5_7_1();
//        test_5_7_2();
//        test_5_7_3();
//        test_5_7_4();
//        test_5_7_5();
//        test5_4();
//        long a = 1000000000L * 34 * 2;
//        System.out.println("===>" + a);
        test(2147483647);
        System.out.println("123123");
    }

    /**
     * 由值创建流
     */
    static void test_5_7_1(){
        Stream<String> stream = Stream.of("Java 8", "Lambdas", "In", "Out", "Action", "123");
        stream.map(String::toUpperCase).forEach(System.out::println);
    }

    /*    * 由数组创建流
     *
     */
    static void test_5_7_2(){
        List<Integer> numbers = Arrays.asList(5, 4, 3, 2, 1);
        numbers.toArray();
        int sum = Arrays.stream(numbers.toArray()).mapToInt(Object::hashCode).sum();
        System.out.println(sum);
        int[] numbers2 = {6, 5, 4, 3, 2, 1};
        int sum2 = Arrays.stream(numbers2).sum();
        System.out.println(sum2);
    }

    /**
     * 文件生成流
     */
    static void test_5_7_3(){
        long uniqueWords = 0;
        try(Stream<String> lines = Files.lines(Paths.get("D://data.txt"), Charset.defaultCharset())){
            lines.flatMap(s -> Arrays.stream(s.split(" ")))
                    .distinct()
                    .forEach(System.out::println);
//                    .count();
            System.out.println("======>" + uniqueWords);

        } catch (IOException e){
            System.out.println(e.getStackTrace());
        }
    }

    /**
     * 创建无限流
     */
    static void test_5_7_4(){
        Stream.iterate(0, integer -> integer + 2)
                .limit(5)
                .forEach(System.out::println);
    }

    static void test(int x){
        Stream.iterate(0, integer -> integer + 1)
                .filter(integer -> integer.toString().indexOf("1") >= 0)
                .limit(x)
                .count();
//                .forEach(System.out::println);
    }

    static void test_5_7_5(){
        Stream.generate(Math::random)
                .limit(5)
                .forEach(System.out::println);
        IntStream ones = IntStream.generate(() -> 1);
        ones.limit(5).forEach(System.out::println);
        IntStream twos = IntStream.generate(new IntSupplier() {
            @Override
            public int getAsInt() {
                return 2;
            }
        });
        twos.limit(5).forEach(System.out::println);

        IntSupplier fib = new IntSupplier() {
            private int previous = 0;
            private int current = 1;
            @Override
            public int getAsInt() {
                int oldPrevious = this.previous;
                int nextValue = this.previous + this.current;
                this.previous = this.current;
                this.current = nextValue;
                return oldPrevious;
            }
        };

        IntStream.generate(fib).limit(10).forEach(System.out::println);
    }

    /**
     * 测验5.4：斐波那契数列
     */
    static void test5_4(){
        Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
                .limit(10)
                .map(ints -> "(" + ints[0] + "," + ints[1] + ")")
                .forEach(System.out::println);
    }

}
